import pyautogui
import pyvirtualcam
import numpy as np
import cv2

# 获取屏幕分辨率
screen_width, screen_height = pyautogui.size()

# 设置虚拟摄像头分辨率（确保与实际屏幕分辨率一致，或者调整为固定值）
virtual_cam_width = screen_width
virtual_cam_height = screen_height

# 初始化虚拟摄像头
with pyvirtualcam.Camera(width=virtual_cam_width, height=virtual_cam_height, fps=30) as cam:
    print(f"正在使用虚拟摄像头: {cam.device}")
    while True:
        # 捕获屏幕内容
        screenshot = pyautogui.screenshot()

        # 将截图转换为 NumPy 数组
        frame = np.array(screenshot)

        # 将图像从 RGB 转换为 BGR（OpenCV 格式）
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        # 调整帧大小以匹配虚拟摄像头分辨率
        frame_resized = cv2.resize(frame, (virtual_cam_width, virtual_cam_height), interpolation=cv2.INTER_LINEAR)

        # 将帧发送到虚拟摄像头
        cam.send(frame_resized)

        # 等待下一帧
        cam.sleep_until_next_frame()
